﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Data;
using System.Threading;

namespace Server
{
    class SocKet
    {
        private const int BUFFER_SIZE = 1024;
        private const int PORT_NUMBER = 2008;
         public int n = 2;
         public string[] NameCL = new string[2];

       
       //  Tau s;
        static ASCIIEncoding encoding = new ASCIIEncoding();

        //IPAddress[] address = new IPAddress[3];//.Any;
        IPAddress address;
        //TcpListener[] listener = new TcpListener[3];
        TcpListener listener;

        Socket[] socket = new Socket[3];
        Stream[] stream= new Stream[3];

       

        public string str = "";
        /// <summary>
        /// Ket noi cac client
        /// </summary>
        public void Op()
        {
            try
            {
                address = IPAddress.Any;
                listener = new TcpListener(address, PORT_NUMBER);
                listener.Start();
                n = 2;
                for (int i = 0; i < n; i++)
                {
                    socket[i] = listener.AcceptSocket();
                }
                for (int j = 0; j < n; j++)
                {
                    stream[j] = new NetworkStream(socket[j]);
                    var writer = new StreamWriter(stream[j]);
                    var reader = new StreamReader(stream[j]);
                    writer.AutoFlush = true;
                    writer.WriteLine((j).ToString());
                }

                NameCL[0] = Receive(0);
                NameCL[1] = Receive(1);

                send(1,NameCL[0].ToString());
                send(0,NameCL[1].ToString());

            }
            catch (Exception ex)
            {
                //Console.WriteLine("Error: " + ex);
            }
        }

        public string Receive(int x)// Nhan chuoi tu client gui wa
        {
            try
            {
                var reader = new StreamReader(stream[x]);
                string st = reader.ReadLine();
                return st;
            }
            catch (Exception ex)
            {
                return "";
                //Console.WriteLine("Error: " + ex);
            }
        }

        public void close(int x)
        {
            stream[x].Close();
            socket[x].Close();
            listener.Stop();
        }


        public void send(int x, string temp)// gui chuoi qua clien
        {

            try
            {
                var reader = new StreamReader(stream[x]);
                var writer = new StreamWriter(stream[x]);
                writer.AutoFlush = true;
                writer.WriteLine(temp);
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Error: " + ex);
            }
        }

        public string client(int x)
        {
            return socket[x].RemoteEndPoint.ToString();// lay thong tin client
        }

    }
}
